package com.rachel.studyapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rachel.studyapp.R;
import com.rachel.studyapp.activity.HistoryDetailActivity;
import com.rachel.studyapp.activity.MainActivity;
import com.rachel.studyapp.adapter.RBaseAdapter;
import com.rachel.studyapp.adapter.RBaseViewholder;
import com.rachel.studyapp.json.funjson.ResultBean;
import com.rachel.studyapp.json.funjson.TodayBean;
import com.rachel.studyapp.mvp.ContractUtils;
import com.rachel.studyapp.mvp.present.FunPresent;
import com.rachel.studyapp.view.CusViewPagerForFun;

import java.util.List;

/**
 * Created by zhengshaorui on 2017/5/23.
 */

public class FunFragment extends BaseFragment<FunPresent> implements ContractUtils.IFunView {
    private static final String TAG = "zsr";
    //开眼首页 url
    private static final String ROOT_URL = "http://baobab.kaiyanapp.com/api/v4/tabs/selected";

    /**
     * ui
     */
    private View mFunView;
    private MainActivity mActivity;
    private RecyclerView mRecyclerView;
    private CusViewPagerForFun mCusViewPagerForFun;
    private FunPresent mPresent;
    private LayoutInflater mLayoutInflater;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mActivity = (MainActivity) getActivity();
        if (mFunView == null) {
            mActivity = (MainActivity) getActivity();
            mFunView = inflater.inflate(R.layout.fragment_fun, container, false);

        }
        initView(mFunView);
        return mFunView;
    }

    private void initView(View view) {
        mCusViewPagerForFun = (CusViewPagerForFun) view.findViewById(R.id.cusviewpagerforfun);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.fun_recyclerview);
        CollapsingToolbarLayout collapsingToolbarLayout =
                (CollapsingToolbarLayout) view.findViewById(R.id.fun_collaps);
        collapsingToolbarLayout.setExpandedTitleGravity(Gravity.CENTER);
        collapsingToolbarLayout.setCollapsedTitleGravity(Gravity.CENTER);
        /**
         * data init
         */
        mLayoutInflater = LayoutInflater.from(mActivity);

    }

    @Override
    public FunPresent initPresent() {
        mPresent = new FunPresent(this,mActivity);
        return mPresent;
    }

    @Override
    public void getJsonDataSuccess(TodayBean rootbean) {
        final List<ResultBean> results = rootbean.getResult();
        if (results != null && !results.isEmpty()){
            RBaseAdapter<ResultBean> adapter = new RBaseAdapter<ResultBean>(
                    R.layout.recyclerview_item,results) {
                @Override
                public void getConver(RBaseViewholder holder, ResultBean data) {
                    holder.setText(R.id.rey_title,data.getTitle())
                            .setText(R.id.rey_des,data.getDes())
                            .setText(R.id.rey_time,data.getLunar())
                            .setDrawable(R.id.rey_icon,data.getPic());

                }
            };
            mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
            mRecyclerView.setAdapter(adapter);

            adapter.setOnItemClickListener(new RBaseAdapter.onItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(mActivity, HistoryDetailActivity.class);
                    intent.putExtra("url",results.get(position).getPic());
                    intent.putExtra("title",results.get(position).getTitle());
                    intent.putExtra("content",results.get(position).getDes());
                    startActivity(intent);
                }
            });
        }
    }

    @Override
    public void onFailure(Object errorObj) {
        Log.d(TAG, "onFailure: "+errorObj.toString());
    }
}
